package com.zhs.gccs.utils;

import android.app.Activity;
import android.content.ActivityNotFoundException;
import android.content.Intent;
import android.net.Uri;
import android.os.Build;

import androidx.core.content.FileProvider;

import java.io.File;

//https://blog.csdn.net/weixin_30448603/article/details/98814855
public class SystemFileManager {
    public static void openAssignFolder(Activity activity,String path){
        File file = new File(path);
        if(null==file || !file.exists()){
            return;
        }
        if(file.isFile()){
            file = file.getParentFile();
        }
        Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
        intent.addCategory(Intent.CATEGORY_DEFAULT);
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        Uri fileUri;
        if (Build.VERSION.SDK_INT >=Build.VERSION_CODES.N){
            fileUri = FileProvider.getUriForFile(activity,
                    activity.getApplicationContext().getPackageName() + ".fileprovider",file);
        }else {
            fileUri = Uri.fromFile(file);
        }
        intent.setDataAndType(fileUri, "file/*");
        try {
            activity.startActivity(intent);
//            startActivity(Intent.createChooser(intent,"选择浏览工具"));
        } catch (ActivityNotFoundException e) {
            e.printStackTrace();
        }
    }
}
